<?php
declare(strict_types=1);

namespace App\Service;

use App\Model\Enum\RiskRuleBlackTypeEnum;
use App\Utils\CustomUtils;
use App\Utils\StringUtils;
use Hyperf\Di\Annotation\Inject;
use App\Model\Dao\RiskRuleBlacktextDao;
use App\Exception\XZXValidationException;
use App\Constants\XZXResponseCode;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Utils\Coroutine;
use App\Utils\DateUtils;
use Hyperf\Utils\Arr;

class RiskRuleBlacktextService {

    /**
     * @Inject()
     * @var RiskRuleBlacktextDao
     */
    private $RiskRuleBlacktextDao;

    /**
     * 列表
     *
     * @param array $param
     *
     * @return array
     */
    public function list (array $param) {

        $limit = $param['limit'];
        $page  = $param['page'];

        $total = $this->RiskRuleBlacktextDao->getCount($param);
        $list  = $this->RiskRuleBlacktextDao->getList($param, $page, $limit);

        $list = CustomUtils::formatCreateTime($list);
        foreach ($list as $k => $v) {
            // $v['createTime'] = xeGetDateTime($v['createTime']);
            $v['strBlackType']    = $this->RiskRuleBlacktextDao->strType($v['blackType']);
            $list[$k] = $v;
        }
        $data = [
            'total' => $total,
            'list'  => $list,
        ];

        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE, XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE), $data);
    }

    /**
     * 详情
     *
     * @param $param
     *
     * @return array
     */
    public function get ($param) {

        $result = $this->RiskRuleBlacktextDao->get($param['id']);

        if (!$result) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DATA_NOT_FOUND));
        }

        $result = CustomUtils::formatCreateTime($result);

        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE, XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE), $result);
    }

    /**
     * 从参数过滤得到添加/更新数组
     *
     * @param $param
     *
     * @return array
     */
    protected function _filterUpdate ($param) {

        $update = Arr::only($param, [
            'status',
            'blackType',
            'blackText',
        ]);

        return $update;
    }

    /**
     * 添加
     *
     * @param $param
     *
     * @return array
     * @throws \Throwable
     */
    public function add ($param) {

        $update = $this->_filterUpdate($param);
        $update = array_merge($update, Arr::only($param, ['createBy', 'updateBy']));

        $update['id'] = StringUtils::uuidV4();

        $this->RiskRuleBlacktextDao->insert($update);

        $data = ['id' => $update['id']];

        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE, XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE), $data);
    }

    /**
     * 编辑
     *
     * @param $param
     *
     * @return array
     */
    public function edit ($param) {

        $id = Arr::pull($param, 'id');

        $info = $this->RiskRuleBlacktextDao->get($id);
        if (!$info) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DATA_NOT_FOUND));
        }

        $update = $this->_filterUpdate($param);
        $update = array_merge($update, Arr::only($param, ['updateBy']));

        $this->RiskRuleBlacktextDao->update($id, $update);

        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE, XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE), CustomUtils::xzxEmptyObject());
    }

    /**
     * 删除
     *
     * @param $param
     *
     * @return array
     */
    public function del ($param) {

        $result = $this->RiskRuleBlacktextDao->get($param['id']);

        if (!$result) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DATA_NOT_FOUND));
        }

        $this->RiskRuleBlacktextDao->del($param['id'], false);

        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE, XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE), CustomUtils::xzxEmptyObject());
    }

    public function blackTypeOptions (array $param) {
        $data = RiskRuleBlackTypeEnum::born()->getOptions();
        return CustomUtils::buildSuccess($data);
    }


}
